package dk.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ReturnData.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import dk.beans.Requestlog;
import dk.mapper.RequestLogMapper;

@RestController
public class RequestLogController {
@Autowired
RequestLogMapper requestlogmapper;
//分页获取所有信息 并倒序输出
@PostMapping("GetRequestLogByPage")
public ResponseData GetRequestLog(Integer pagenum,Integer count) {
	ResponseData data = new ResponseData();
	IPage<Requestlog> page = new Page(pagenum,count);
	page = requestlogmapper.selectPage(page, new QueryWrapper<Requestlog>().orderByDesc("RequestCreateTime"));
	data.setCode(200);
	data.setMessage("分页查询日志成功");
	data.setData(page);
	return data;
	
}
//获取一共多少服务
@PostMapping("GetRequestLogAfterGroupByServerName")
public ResponseData GetRequestLogAfterGroupBy() {
	ResponseData data = new ResponseData();
	
	List<Requestlog> list=requestlogmapper.selectList(new QueryWrapper<Requestlog>().select("ServerName").groupBy("ServerName"));
	data.setCode(200);
	data.setMessage("分组查询日志成功");
	data.setData(list);
	return data;
}
//根据服务名获取请求数获取服务请求数
@PostMapping("GetCountByServerName")
public ResponseData GetCountByServerName(String ServerName) {
	ResponseData data = new ResponseData();
	int  count = requestlogmapper.selectCount(new QueryWrapper<Requestlog>().eq("ServerName",ServerName));
	data.setCode(200);
	data.setMessage("查询"+ServerName+"数量成功");
	data.setData(count);
	return data;
}
@PostMapping("GetAllCount")
public ResponseData GetAllCount() {
	ResponseData data = new ResponseData();
	
	List<Requestlog> list=requestlogmapper.selectList(new QueryWrapper<Requestlog>().select("ServerName").groupBy("ServerName"));
	List<Map> reslist = new ArrayList<Map>();
	
	for(Requestlog requestlog : list) {
		Map<String,Object> resmap = new HashMap<String, Object>();
		resmap.put("name", requestlog.getServerName());
		resmap.put("count",requestlogmapper.selectCount(new QueryWrapper<Requestlog>().eq("ServerName",requestlog.getServerName())) );
		reslist.add(resmap);
	}
	
	data.setMessage("获取全部数量成功");
	data.setCode(200);
	data.setData(reslist);
	return data;
}
}
